import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 14-4-26.
 */
public class Domain {
    static String base_url="http://pandavip.www.net.cn/check/check_ac1.cgi?domain=";
    static String out_file="domain.txt";

    public static void main(String[] args) throws Exception {
        //递归的方法生成对应位数的串
        String str="abcdefghijklmnopqrstuvwxyz";
        char[] result =new char[4];
        getString(str.toCharArray(),4,0,result);
        System.out.println("over");
    }

    /**
     *
     * @param base    数据字典
      * @param length   要生成字符串的位数
     * @param index     当前字符串的索引
     * @param result    生成字符串
     * @throws Exception
     */
    public static void getString(char[] base,int length,int index,char[] result) throws Exception {
        for(int i=0;i<base.length;i++){
            result[index]=base[i];
            if(index==length-1){
                String url=base_url+new String(result,0,length)+".com";
                String json=getJsonString(url);
                if(json.contains("210")){
                    System.out.println(url+"------"+json);
                    FileAppend.method1(out_file, url + "-----" + json + '\n');
                }else{
                    System.out.println(url+"------false");
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                getString(base,length, index+1, result);
            }
        }

    }

    public static String getJsonString(String urlPath) throws Exception {
        URL url = new URL(urlPath);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        //对应的字符编码转换
        Reader reader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(reader);
        String str = null;
        StringBuffer sb = new StringBuffer();
        while ((str = bufferedReader.readLine()) != null) {
            sb.append(str);
        }
        reader.close();
        connection.disconnect();
        return sb.toString();
    }

}
